package com.wdz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wdz.annotations.ResponseResult;
import com.wdz.article.entity.Article;
import com.wdz.article.service.IArticleService;
import com.wdz.category.entity.Category;
import com.wdz.category.service.ICategoryService;
import com.wdz.http.Response;
import com.wdz.resource.entity.Resource;
import com.wdz.resource.service.IResourceService;
import com.wdz.search.SearchGets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
@ResponseResult
public class CategoryController {
    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 根据分类等级查询分类集合
     */
    @PostMapping("/queryListByLevel")
    public Response queryListByLevel(@RequestParam Integer level){
        return Response.success(iCategoryService.selectList(new EntityWrapper<Category>().where("level={0} and type=1",level)));
    }
    /**
     * 根据分类等级分页查询分类集合
     */
    @PostMapping("/queryPageListByLevel")
    public Response queryPageListByLevel(@RequestParam Integer level,@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        Map<String,Object> data = new HashMap<>();
        Wrapper<Category> wrapper = new EntityWrapper<Category>().where("level={0} and type=1",level);
        data.put("total",iCategoryService.selectCount(wrapper));
        data.put("data",iCategoryService.selectPage(new Page<>(pageNo,pageSize),wrapper).getRecords());
        return Response.success(data);
    }
    /**
     * 添加分类
     */
    @PostMapping("/addCategory")
    public Response addCategory(@RequestBody Category category){
        if(iCategoryService.selectCount(new EntityWrapper<Category>().where("name={0} and type=1 and level={1}",category.getName(),category.getLevel())) > 0){
            return Response.error("当前分类等级下的分类名已存在");
        }
        iCategoryService.insert(category);
        return Response.success((Object) category.getId());
    }
    /**
     * 查询单个分类
     */
    @PostMapping("/queryCategory")
    public Response queryCategory(@RequestParam String id){
        Category category = iCategoryService.selectById(id);
        category.setImages(iCategoryService.selectCategoryImagesById(id));
        category.setArticleVolume(iCategoryService.selectCategoryLengthById(id));
        category.setArticles(iCategoryService.selectCategoryArticlesById(id));
        return Response.success(category);
    }
    /**
     * 查询该分类下面的所有分类
     */
    @GetMapping("/queryCategorysById/{id}/{pageNo}/{pageSize}")
    public Response queryCategorysById(@PathVariable String id,@PathVariable Integer pageNo,@PathVariable Integer pageSize){
        Map<String,Object> data = new HashMap<>();
        data.put("total",iCategoryService.selectCount(new EntityWrapper<Category>().where("parent_category_id={0}",id)));
        data.put("data",iCategoryService.selectCategorysById(id,pageNo,pageSize));
        return Response.success(data);
    }
    /**
     * 删除单个分类
     */
    @DeleteMapping("/{id}")
    public Response delete(@PathVariable String id){iCategoryService.deleteById(id); return Response.success();}

    /**
     * 获取全部分类
     */
    @GetMapping("/")
    public Response get(){
        return Response.success(iCategoryService.selectList(null));
    }

    /**
     * 按文章数量排名找出5个一级分类(根据栏目id)
     */
    @GetMapping("/queryCategorysByArticleCount/{id}")
    @ResponseResult
    public List<Map<Object,Object>> queryCategorysByArticleCount(@PathVariable String id) {
        return iCategoryService.selectCategorysByArticleCount(id);
    }
    @Autowired
    private IArticleService iArticleService;
    /**
     * 根据分类id查询所有文章
     */
    @GetMapping("/queryAllArticlesById/{id}/{pageNo}/{pageSize}")
    public Response queryAllArticlesById(@PathVariable String id,@PathVariable Integer pageNo,@PathVariable Integer pageSize){
        Map<String,Object> data = new HashMap<>();
        data.put("total",iArticleService.selectCount(new EntityWrapper<Article>().where("parent_category_id={0}",id)));
        data.put("data",iCategoryService.selectAllArticlesById(id,pageNo,pageSize));
        return Response.success(data);
    }
    @Autowired
    private IResourceService iResourceService;
    /**
     * 根据分类id查询所有资源
     */
    @GetMapping("/queryAllResourcesById/{id}/{pageNo}/{pageSize}")
    public Response queryAllResourcesById(@PathVariable String id,@PathVariable Integer pageNo,@PathVariable Integer pageSize){
        Map<String,Object> data = new HashMap<>();
        data.put("total",iResourceService.selectCount(new EntityWrapper<Resource>().where("parent_category_id={0}",id)));
        data.put("data",iCategoryService.selectAllResourcesById(id,pageNo,pageSize));
        return Response.success(data);
    }

    /**
     * 分页查询分类列表 可搜索
     * @param pageNo 起始页
     * @param pageSize 页码
     * @param category 查询对象
     * @param needPage 是否需要分页
     * @return total 总数 data 数据列表
     */
    @PostMapping("/gets")
    public Map<String,Object> gets(Integer pageNo,Integer pageSize,Category category,Boolean needPage){
        /*EntityWrapper<Category> entityWrapper = new EntityWrapper<>();
        if (needPage == null) {
            needPage = Boolean.valueOf(true);
        }
        if (pageNo == null || pageNo.intValue() <= 0) {
            pageNo = Integer.valueOf(1);
        }
        if (pageSize == null || pageSize.intValue() <= 0) {
            pageSize = Integer.valueOf(10);
        }
        if(category.getName() != null){
            entityWrapper.like("name",category.getName().trim());
        }
        if(category.getMainCategoryId() !=null){
            entityWrapper.eq("main_category_id",category.getMainCategoryId().trim());
        }
        if(category.getParentCategoryId() != null){
            entityWrapper.eq("parent_category_id",category.getParentCategoryId().trim());
        }
        if(category.getLevel() != null){
            entityWrapper.like("level",category.getLevel().toString().trim());
        }
        if(createDateLe != null){
            entityWrapper.le("create_date",createDateLe);
        }
        if(createDateGe != null){
            entityWrapper.ge("create_date",createDateGe);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",iCategoryService.selectCount(entityWrapper));
        if(needPage.booleanValue()){
            map.put("data",iCategoryService.selectPage(new Page<>(pageNo,pageSize),entityWrapper).getRecords());
        }else{
            map.put("data",iCategoryService.selectList(entityWrapper));
        }*/
        return SearchGets.gets(iCategoryService,category,pageNo,pageSize,needPage);
    }

}
